<?php
// +----------------------------------------------------------------------
// | word2layui
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://saruri.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( MIT LICENSE )
// +----------------------------------------------------------------------
// | Author: saruri <sarurifan@gmail.com>
// +----------------------------------------------------------------------
namespace saruri;

include_once "datatransform.php";

class MainService
{
    /**
     * @var array 配置参数
     */
    private $_config = [
        'width'=>1200,
    ];

    public $result;

    private $_requestString;

    public $jsonResult; //返回的最终数组

    //初始
    public function __construct($string='')
    {
        $this->_config['requestString']=$string;
        //各种配置
        date_default_timezone_set('Asia/Shanghai');
        if ($string=='') {
            return  $this->err();
        }
        //接收字符串
        self::init();
        //exit("shanghai");
    }
    
    //初始化参数
    public function init()
    {
        $arr=[];
        $md= new DataTransform($this->_config);
        $type=$_POST['type'];
        //exit($type);
        $type || exit("选择类型");
        if($type=='json'){
            $this->result=$md->base($type)->strip()->th()->td()->com()->run();
        }
        if($type=='tab'){
            $this->result=$md->base($type)->strip()->th()->tab();
        }

        if($type=='tab_input'){
            $this->result=$md->base($type)->strip()->th()->tabInput();
        }

        if($type=='layui'){
            $this->result=$md->base($type)->strip()->th()->td()->com()->layui();
        }
        
        //var_dump($this->result);
        //exit("测试");
        //异常判断
        //self::check();
    }

    /*
    *  @desc      错误提示
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 11:14:10
    */
    public function err()
    {
        //do somehing
        $arr=[
            'status'=>false,
            'msg'=>'错误!'
        ];
        return json_encode($arr);
    }

    //重载配置
    public function setConfig($config)
    {
        self::$_config=$config;
    }
    

    //执行
    public function run()
    {
        exit(json_encode($this->result));
    }
}

//注释一下 kanances
$string=$_POST['md'];

$md= new MainService($string);
$md->run();
